#include <stdio.h>
#include <unistd.h>
#include <sys/resource.h>
#include <sys/wait.h>
#include <stdlib.h>
int main(){
	rusage info;
	rlimit tlim;
	rlimit mlim;
	//set complie time 1s
	tlim.rlim_cur =1;
	tlim.rlim_max =1;
	int stat;
	pid_t work=vfork();	
	if (work==0){
		setrlimit(RLIMIT_CPU,&tlim);
		printf("pid : %d\n",getpid());
		//system("./run.o");
		/*
			阻塞和超时不一样。scanf() 会一直停了 ，不会占用运行时间
		*/
		freopen("a.txt","r",stdin);
		execlp("./run.o","./run.o",NULL);
		//system("./run.o < a.txt");
		/*
			用system 可重定向 ， 但是被终止时stat 还是 0 
			exec 不能重定向 ，被终止时stat 改变
		*/
		//every code at here cannot be exec
		exit(0); //exit(getpid()) is different
	}else{
		wait4(work,&stat,NULL,&info);
		printf("pid : %d\n",work);
		printf("%ld.%06ld  %ld.%06ld\n",info.ru_utime.tv_sec,info.ru_utime.tv_usec,info.ru_stime.tv_sec,info.ru_stime.tv_usec);
		int ps = getpagesize();
		//printf("page size: %d\n",ps);
		int memused = ps*info.ru_minflt;
		printf("mem used: %d\n",memused);
		printf("stat: %d\n",stat);
	}
	return 0;	
}
